package com.ydd.controller.admin;

import com.ydd.constant.MessageConstant;
import com.ydd.dto.YddArticleDto;
import com.ydd.dto.YddArticlePageDto;
import com.ydd.dto.YddCateDto;
import com.ydd.dto.YddCatePageDto;
import com.ydd.entity.YddArticle;
import com.ydd.entity.YddCate;
import com.ydd.result.Result;
import com.ydd.service.YddArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/ydd_article")
@Slf4j
public class YddArticleController {
    @Autowired
    private YddArticleService yddArticleService;

    /**
     * 添加文章
     *
     * @param yddArticle
     * @return
     */
    @PostMapping("/add")
    public Result<YddArticle> addArticle(@RequestBody YddArticle yddArticle) {
        boolean result = yddArticleService.addArticle(yddArticle);
        if (result) {
            return Result.success(yddArticle);
        } else {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
    }

    /**
     * 根据id删除文章
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public Result<Map<String, Object>> deleteById(@RequestParam("id") Long id) {
        Map<String, Object> responseData = new HashMap<>();
        responseData.put("result", yddArticleService.deleteById(id));
        return Result.success(responseData);
    }

    /**
     * 文章分类详情
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public Result<Map<String, Object>> getArticleById(@RequestParam("id") Long id) {
        YddArticleDto yddArticleDto = yddArticleService.getCateById(id);

        Map<String, Object> responseData = new HashMap();

        responseData.put("id", yddArticleDto.getId());
        responseData.put("title", yddArticleDto.getTitle());
        responseData.put("author", yddArticleDto.getAuthor());
        responseData.put("keywords", yddArticleDto.getKeywords());
        responseData.put("description", yddArticleDto.getDescription());
        responseData.put("content", yddArticleDto.getContent());
        responseData.put("pic", yddArticleDto.getPic());
        responseData.put("click", yddArticleDto.getClick());
        responseData.put("ishot", yddArticleDto.getIshot());
        responseData.put("istop", yddArticleDto.getIstop());
        responseData.put("creatdate", yddArticleDto.getCreatdate());
        responseData.put("updatedate", yddArticleDto.getUpdatedate());
        responseData.put("cateid", yddArticleDto.getCateid());
        responseData.put("tags", yddArticleDto.getTags());
        responseData.put("status", yddArticleDto.getStatus());
        responseData.put("lovenum", yddArticleDto.getLovenum());

        return Result.success(responseData);
    }

    /**
     * 修改文章
     *
     * @param yddArticle
     * @return
     */
    @PostMapping("/update")
    public Result<YddArticle> updateArticle(@Valid @RequestBody YddArticle yddArticle) {
        if (yddArticleService.updateArticle(yddArticle)) {
            return Result.success(yddArticle);
        } else {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
    }

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @GetMapping("/list")
    public Result YddArticleList(YddArticlePageDto dto) {

        return yddArticleService.YddArticleList(dto);
    }
}

